package com.conf;

import com.util.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {

    private final static Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @Value("${spring.servlet.multipart.max-file-size}")
    String maxFileSize;

    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.OK)
    public AjaxResult bindException(BindException exception) {
        log.info("BindException");
        return AjaxResult.getFail(exception.getBindingResult().getFieldError().getDefaultMessage());
    }

    @ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.OK)
    public AjaxResult methodArgumentNotValidException(MethodArgumentNotValidException exception) {
        log.info("MethodArgumentNotValidException");
        return AjaxResult.getFail(exception.getBindingResult().getFieldError().getDefaultMessage());
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseStatus(HttpStatus.OK)
    public AjaxResult methodArgumentNotValidException(MissingServletRequestParameterException e) {
        log.error("参数异常",e);
        return AjaxResult.getFail(e.getMessage());
    }

    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseStatus(HttpStatus.OK)
    public AjaxResult methodArgumentNotValidException(ConstraintViolationException e) {
        log.error("异常",e);
        for (ConstraintViolation<?> constraintViolation : e.getConstraintViolations()) {
            return AjaxResult.getFail(constraintViolation.getMessage());
        }
        return AjaxResult.getFail(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public AjaxResult exception(Exception e) {
        log.error("执行异常",e);
        if(e.getMessage() == null){
            return AjaxResult.getFail("空异常");
        }
        if(e.getMessage().contains("Duplicate entry")){
            return AjaxResult.getFail("已存在");
        }
        if(e.getMessage().contains("is not present")){
            log.error("缺少参数",e);
            return AjaxResult.getFail("缺少参数");
        }
        return AjaxResult.getFail(e.getMessage());
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseStatus(HttpStatus.PAYLOAD_TOO_LARGE)
    public AjaxResult handlerAnyException(MaxUploadSizeExceededException e) {
        log.error("上传文件过大",e);
        return AjaxResult.getFail("上传文件超过"+maxFileSize);
    }

}
